package com.hdjk.view.callback;

import com.hdjk.view.entity.FinancialEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class FinancialListCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg=super.parseNetworkResponse(response, i);
        JSONObject object= (JSONObject) msg.getData();
        if (object!=null) {
            if (!object.isNull("data")) {
                JSONArray array = object.getJSONArray("data");
                if (array != null && array.length() > 0) {
                    List<FinancialEntity> list = new ArrayList<>();
                    for (int j = 0; j < array.length(); j++) {
                        JSONObject jsonObject = array.getJSONObject(j);
                        FinancialEntity entity = new FinancialEntity();
                        if (!jsonObject.isNull("subProductName")) {
                            entity.setSubProductName(jsonObject.getString("subProductName"));
                        }
                        if (!jsonObject.isNull("subShortName")) {
                            entity.setSubShortName(jsonObject.getString("subShortName"));
                        }
                        if (!jsonObject.isNull("productCode")) {
                            entity.setProductCode(jsonObject.getString("productCode"));
                        }
                        if (!jsonObject.isNull("revenueMode")) {
                            entity.setRevenueMode(jsonObject.getString("revenueMode"));
                        }
                        if (!jsonObject.isNull("status")) {
                            entity.setStatus(jsonObject.getString("status"));
                        }
                        if (!jsonObject.isNull("isBuyEnable")) {
                            entity.setIsBuyEnable(jsonObject.getString("isBuyEnable"));
                        }
                        if (!jsonObject.isNull("appointmentBuyEnable")) {
                            entity.setAppointmentBuyEnable(jsonObject.getString("appointmentBuyEnable"));
                        }
                        if (!jsonObject.isNull("appointmentStartDate")) {
                            entity.setAppointmentStartDate(jsonObject.getString("appointmentStartDate"));
                        }
                        if (!jsonObject.isNull("appointmentEndDate")) {
                            entity.setAppointmentEndDate(jsonObject.getString("appointmentEndDate"));
                        }
                        if (!jsonObject.isNull("minInitialBuyAmount")) {
                            entity.setMinInitialBuyAmount(jsonObject.getString("minInitialBuyAmount"));
                        }
                        if (!jsonObject.isNull("minInitialIpoAmount")) {
                            entity.setMinInitialIpoAmount(jsonObject.getString("minInitialIpoAmount"));
                        }
                        if (!jsonObject.isNull("operationPeriodLength")) {
                            entity.setOperationPeriodLength(jsonObject.getString("operationPeriodLength"));
                        }
                        if (!jsonObject.isNull("buyStartDate")) {
                            entity.setBuyStartDate(jsonObject.getString("buyStartDate"));
                        }
                        if (!jsonObject.isNull("buyEndDate")) {
                            entity.setBuyEndDate(jsonObject.getString("buyEndDate"));
                        }
                        if (!jsonObject.isNull("closeStartDate")) {
                            entity.setCloseStartDate(jsonObject.getString("closeStartDate"));
                        }
                        if (!jsonObject.isNull("closeEndDate")) {
                            entity.setCloseEndDate(jsonObject.getString("closeEndDate"));
                        }
                        if (!jsonObject.isNull("annualPerformanceBenchMark")) {
                            entity.setAnnualPerformanceBenchMark(jsonObject.getString("annualPerformanceBenchMark"));
                        }
                        if (!jsonObject.isNull("nav")) {
                            double l = jsonObject.getDouble("nav");
                            BigDecimal b = new BigDecimal(l);
                            entity.setNav(b.setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                        }
                        if (!jsonObject.isNull("navDate")) {
                            entity.setNavDate(jsonObject.getString("navDate"));
                        }
                        if (!jsonObject.isNull("accumulateNav")) {
                            double l = jsonObject.getDouble("accumulateNav");
                            BigDecimal b = new BigDecimal(l);
                            entity.setAccumulateNav(b.setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                        }
                        if (!jsonObject.isNull("earningsPer10000")) {
                            double l = jsonObject.getDouble("earningsPer10000");
                            BigDecimal b = new BigDecimal(l);
                            entity.setEarningsPer10000(b.setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                        }
                        if (!jsonObject.isNull("sevenDaysAnnualizedYield")) {
                            double l = jsonObject.getDouble("sevenDaysAnnualizedYield");
                            BigDecimal b = new BigDecimal(l);
                            entity.setSevenDaysAnnualizedYield(b.setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                        }
                        list.add(entity);
                    }
                    msg.setData(list);
                } else {
                    msg.setData("");
                }
            } else {
                msg.setData("");
            }
        }
        return msg;
    }
}
